#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import unittest

from jute.db import ColumnFamily,Column

class   ColumnFamily01TestCase(unittest.TestCase) :
    def setUp(self) :
        pass
        ## setUp()

    def tearDown(self) :
        pass
        ## tearDown()

    def testSingleColumn(self) :
        cf = ColumnFamily('SimpleCF', 'SIMPLE')
        c = Column('C','Hello,Column',1234)
        cf.addColumn(c)
        serialized_cf = ColumnFamily.Serializer.serialize(cf)
        cf = None

        cf = ColumnFamily.Serializer.deserialize(serialized_cf)

        self.assertFalse(cf is None)
        self.assertEqual(cf.name, 'SimpleCF')
        ## testSingleColumn()

    def testGetColumnCount(self) :
        cf = ColumnFamily()
        val = 'sample values'

        c = Column('col1', val, 1)
        cf.addColumn(c)
        c = Column('col2', val, 2)
        cf.addColumn(c)
        c = Column('col1', val, 3)
        cf.addColumn(c)

        self.assertEqual(2, cf.getColumnCount())
        ## testGetColumnCount()

    def testManyColumns(self) :
        cf = ColumnFamily()

        items = {}
        for idx in range(256) :
            items[ 'hello-%04d' % idx ] = 'world----%09d' % idx 

        for key,value in items.iteritems() :
            c = Column(key, value, 1234)
            cf.addColumn(c)
        serialized_cf = ColumnFamily.Serializer.serialize(cf)
        
        cf_back = ColumnFamily.Serializer.deserialize(serialized_cf)
        for key,value in items.iteritems() :
            c = cf.getColumn(key)
            self.assertEqual(c.value, value)
        ## testManyColumns()

    def testTimestamp(self) :
        cf = ColumnFamily()

        val1 = 'value 1'
        val2 = 'value 2'
        val3 = 'value 3'

        c = Column('col1', val1, 2)
        cf.addColumn(c)
        c = Column('col1', val2, 2)     # same timestamp, new value
        cf.addColumn(c)
        c = Column('col1', val3, 1)     # older timestamp -- should be ignored
        cf.addColumn(c)

        self.assertEqual(val2, cf.getColumn('col1').value)
        ## testTimestamp()

    def testMergeAndAdd(self) :
        self.assertTrue(False)
        ## testMergeAndAdd()

    ## class ColumnFamily01TestCase


if __name__ == '__main__' :
    unittest.main()

